<template>
  <div class="chat-container" ref="chatContainer">
    <div class="messages-list">
      <ChatMessage 
        v-for="(message, index) in messages"
        :key="index"
        :message="message"
      />

      <!-- 加载状态 -->
      <div v-if="isLoading" class="message ai-message">
        <div class="message-bubble">
          <div class="typing-indicator">
            <div class="dot"></div>
            <div class="dot"></div>
            <div class="dot"></div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import ChatMessage from './ChatMessage.vue';

const props = defineProps({
  messages: {
    type: Array,
    required: true
  },
  isLoading: {
    type: Boolean,
    default: false
  }
});

const chatContainer = ref(null);

// 滚动到底部的方法
const scrollToBottom = () => {
  if (chatContainer.value) {
    chatContainer.value.scrollTop = chatContainer.value.scrollHeight;
  }
};

// 监听消息变化，自动滚动到底部
watch(() => props.messages, () => {
  nextTick(() => {
    scrollToBottom();
  });
}, { deep: true });

// 监听加载状态变化，自动滚动到底部
watch(() => props.isLoading, () => {
  nextTick(() => {
    scrollToBottom();
  });
});

// 暴露方法给父组件
defineExpose({
  scrollToBottom
});
</script>

<style scoped>
.chat-container {
  flex: 1;
  overflow-y: auto;
  padding: 10px 16px;
  box-sizing: border-box;
}

.messages-list {
  padding: 16px 0;
}

.message {
  margin-bottom: 16px;
  display: flex;
}

.ai-message {
  justify-content: flex-start;
}

.message-bubble {
  max-width: 70%;
  padding: 12px 16px;
  border-radius: 18px;
  position: relative;
  background-color: #ffffff;
  color: #1c1c1e;
  border-bottom-left-radius: 6px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}

/* 加载动画 */
.typing-indicator {
  display: flex;
  align-items: center;
  gap: 4px;
}

.dot {
  width: 8px;
  height: 8px;
  background-color: #8e8e93;
  border-radius: 50%;
  animation: typing 1.4s infinite ease-in-out;
}

.dot:nth-child(1) {
  animation-delay: -0.32s;
}

.dot:nth-child(2) {
  animation-delay: -0.16s;
}

@keyframes typing {
  0%,
  80%,
  100% {
    transform: scale(0.8);
    opacity: 0.5;
  }
  40% {
    transform: scale(1);
    opacity: 1;
  }
}

/* 滚动条样式 */
.chat-container::-webkit-scrollbar {
  width: 4px;
}

.chat-container::-webkit-scrollbar-track {
  background: transparent;
}

.chat-container::-webkit-scrollbar-thumb {
  background: #c7c7cc;
  border-radius: 2px;
}

.chat-container::-webkit-scrollbar-thumb:hover {
  background: #8e8e93;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .chat-container {
    padding: 10px 12px;
  }
}
</style> 